package com.chinasofti.controller;

import com.chinasofti.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    //localhost:8080/
    //localhost:8080/login
    @GetMapping(value = {"/","/login"})
    public String loginpage(){
        return "login"; // login.html
    }
    @PostMapping("/login")
    public String main(HttpSession session, Model model, User user){
        if(StringUtils.hasLength(user.getUsername()) && "123".equals(user.getPassword())){
            session.setAttribute("loginUser",user);
            return "redirect:index.html";
        }else {
            model.addAttribute("msg","账号名或密码错误");
            return "login";
        }
    }
    @GetMapping("index.html")
    public String toIndex(){
        return "index"; // login.html
    }
}
